package com.cavytech.wear;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;

public class SettingsActivity extends PreferenceActivity {

    private static final boolean ALWAYS_SIMPLE_PREFS = false;
    private boolean mHidEnableChanged = false;
    private boolean mLinkLostChanged = false;

    Preference mHidControlPref;
    CheckBoxPreference mHidEnablePref;
    CheckBoxPreference mLinkLostPref;

    private Preference.OnPreferenceClickListener mHidEnableClickLinstener = new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mHidControlPref.setEnabled(mHidEnablePref.isChecked());
            mHidEnableChanged = !mHidEnableChanged;
            return true;
        }
    };

    private Preference.OnPreferenceClickListener mLinkLostLinstener = new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mLinkLostChanged = !mLinkLostChanged;
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupSimplePreferencesScreen();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLinkLostChanged = false;
        mHidEnableChanged = false;
    }

     @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        //setupSimplePreferencesScreen();

         mHidControlPref = findPreference("HidControl");
         mHidEnablePref = (CheckBoxPreference)findPreference("HidEnable");
         mHidEnablePref.setOnPreferenceClickListener(mHidEnableClickLinstener);
         mLinkLostPref = (CheckBoxPreference)findPreference("LinkLostVibrate");
         mLinkLostPref.setOnPreferenceClickListener(mLinkLostLinstener);

         mHidControlPref.setEnabled(mHidEnablePref.isChecked());
     }

    private void setupSimplePreferencesScreen() {
        //if (!isSimplePreferences(this)) {
        //    return;
        //}
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference("SettingDisconnect"));

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            int result = 0;
            if ( mLinkLostChanged ) result += 1;
            if ( mHidEnableChanged ) result += 2;

            Intent intent=new Intent();
            intent.putExtra("LINK_LOST", mLinkLostPref.isChecked()?1:0);
            intent.putExtra("HID_ENABLE", mHidEnablePref.isChecked()?1:0);

            setResult(result, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //@Override
    //public boolean onIsMultiPane() {
    //    return isXLargeTablet(this) && !isSimplePreferences(this);
    //}

    //private static boolean isXLargeTablet(Context context) {
    //    return (context.getResources().getConfiguration().screenLayout
    //            & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    //}

    //private static boolean isSimplePreferences(Context context) {
    //    return ALWAYS_SIMPLE_PREFS
    //            || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
    //            || !isXLargeTablet(context);
    //}

    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference.setSummary(
                        index >= 0
                                ? listPreference.getEntries()[index]
                                : null);

            }  else {
                // For all other preferences, set the summary to the value's
                // simple string representation.
                preference.setSummary(stringValue);
            }
            return true;
        }
    };


    private static void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

}
